package com.flash.BusinessProcess.service.impl;

import com.flash.BusinessProcess.entity.Settlement;
import com.flash.BusinessProcess.mapper.SettlementMapper;
import com.flash.BusinessProcess.service.SettlementService;
import com.flash.CashAdministration.entity.FundsAllot;
import com.flash.CashAdministration.mapper.FundsAllotMapper;
import com.flash.GlobalManagement.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

import static com.flash.GlobalManagement.util.NumberUtil.getNumber;


@Service
public class SettlementServiceImpl implements SettlementService {

    @Resource
    FundsAllotMapper fundsAllotMapper;

    @Resource
    Dbutil dbutil;
    @Autowired
    SettlementMapper settlementMapper;
    @Override
    public List<Settlement> SettlementMap(Settlement settlement, int page, int limit) {
        HashMap map = new HashMap();
        map.put("strWhere", buildWhere(settlement));
        map.put("page", (page - 1) * limit);
        map.put("limit", limit);
        return settlementMapper.SettlementMap(map);
    }

    @Override
    public int selectCountSettlement() {
        return settlementMapper.selectCountSettlement();
    }

    @Override
    public List<Settlement> SettlementMapFan(Settlement settlement, int page, int limit) {
        HashMap map=new HashMap();
        map.put("strWhere", buildWhere(settlement));
        map.put("page", (page - 1) * limit);
        map.put("limit", limit);
        return settlementMapper.SettlementMapFan(map);
    }

    @Override
    public int updateSettlement(String settlement) {
        System.out.println("ssss1");
        int i = 0;
        List<Settlement> settlements = SysUtil.jsonToArrayList(settlement, Settlement.class);
        for (Settlement settlement1:settlements) {
            i = settlementMapper.updateSettlement(settlement1.getDealId());
            fundsAllotMapper.deleteBusinessId(settlement1.getDealId());
        }
        return i;
    }

    @Override
    public int updateSettlementFan(String settlement) {
        System.out.println("sss2");
        int i = 0;
        List<Settlement> settlements = SysUtil.jsonToArrayList(settlement, Settlement.class);
        for (Settlement settlement1:settlements) {

            FundsAllot fundsAllot = new FundsAllot();
//           dbutil.getMaxId(TableIdUtil.FAID, TableNameUtil.FA, TableTimeUtil.FATime    )FATim
            String id=getNumber("fundsAllotId","fundsAllot","FA");
            System.out.println("111111"+id);
            fundsAllot.setFundsAllotId(id);
            fundsAllot.setCashId(settlement1.getCashId());
            fundsAllot.setFundId(settlement1.getFundId());
            fundsAllot.setAllotType(3);
            if (settlement1.getDealWay() ==1 ){
                fundsAllot.setAllotDirection(1);
            }else{
                fundsAllot.setAllotDirection(-1);
            }
            // 调拨金额
          fundsAllot.setAllotMoney(settlement1.getEndMoney());

            fundsAllot.setBusinessId(settlement1.getDealId()); // 业务编号
            fundsAllot.setBusinessTime(settlement1.getDealTime()); // 业务日期
            fundsAllot.setAllotTime(settlement1.getEndTime()); // 调拨日期
            fundsAllot.setAllotRemark("");
            System.out.println(fundsAllot);

            fundsAllotMapper.insertFundsAllot(fundsAllot);
            System.out.println(fundsAllot);
            i = settlementMapper.updateSettlementFan(settlement1.getDealId());
        }
        return i;
    }


    public String buildWhere(Settlement settlement) {
        StringBuffer strWhere = new StringBuffer();
        if (settlement.getDealWay()!=0 ) {
            System.out.println(settlement.getSecurityId());
            strWhere.append(" and d.dealWay like '%" + settlement.getDealWay() + "%'");
        }
        if (settlement.getEndTime() != null && !settlement.getEndTime().equals("")) {
            System.out.println(settlement.getEndTime());
            strWhere.append(" and d.endTime like '%" + settlement.getEndTime() + "%'");

        }
        System.out.println(strWhere+"8");
        return strWhere.toString();
    }
}
